package lib.bone.access.module.dict.impl;

import cn.hutool.core.lang.Validator;
import lib.bone.access.constant.ResearchTypeConst;
import lib.bone.access.module.dict.dao.NepuBookDictDAO;
import lib.bone.access.module.dict.entity.NepuBookDict;
import lib.bone.access.module.dict.DictService;
import lib.bone.common.constant.ResponseCodeConst;
import lib.bone.common.core.domain.PageResultDTO;
import lib.bone.common.core.domain.ResponseDTO;
import lib.bone.common.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * @author  :贾斯淇
 */

@Slf4j
@Service
public class BookDictServiceImpl implements DictService<NepuBookDict,String> {

    @Autowired
    NepuBookDictDAO nepuBookDictDAO;

//    @Autowired
//    NepuBookDAO nepuBookDAO;

    @Override
    public PageResultDTO<NepuBookDict> getAll(Pageable pageable, Example<NepuBookDict> example) {

        Page<NepuBookDict> page;
        //是否为空 --
        if(Validator.isEmpty(example)){
            page = nepuBookDictDAO.findAll(pageable);
        }else {
            page = nepuBookDictDAO.findAll(example,pageable);
        }
        List<NepuBookDict> list = page.getContent();
        PageResultDTO<NepuBookDict> pageResultDTO = new PageResultDTO<>();
        pageResultDTO.setContent(list);
        pageResultDTO.setSize(page.getSize());
        pageResultDTO.setTotalPages(page.getTotalPages());
        pageResultDTO.setNumber(page.getNumber());
        pageResultDTO.setTotalElements(page.getTotalElements());

        return pageResultDTO;
    }

    @Override
    public ResponseDTO<NepuBookDict> deleteById(String id) {
//        Boolean useExist = nepuBookDAO.existsByBookDictId(id);
//        // Long useCount = nepuAwardDAO.countByAwardDictId(id);
//        if(useExist){
//            return ResponseDTO.wrap(ResponseCodeConst.FORBIDDEN,"存在关联该词典的著作信息,拒绝执行删除!");
//        }
        // 2. 判断资源内容是否存在
        Optional<NepuBookDict> nepuBookDictOp = nepuBookDictDAO.findById(id);
        if(!nepuBookDictOp.isPresent()) return ResponseDTO.wrap(ResponseCodeConst.NOT_FOUND,"未找到ID:["+id+"]对应资源!");
        nepuBookDictDAO.deleteById(id);
        return ResponseDTO.succMsg("删除成功!");
    }

    @Override
    public Optional<NepuBookDict> getById(String id) {
            return nepuBookDictDAO.findById(id);
    }

    @Override
    public NepuBookDict saveOne(NepuBookDict nepuBookDict) {
        if (Validator.isNotEmpty(nepuBookDict.getId())){
            throw new BusinessException(ResponseCodeConst.ERROR_PARAM);
        }
        return nepuBookDictDAO.save(nepuBookDict);
    }

    @Override
    public NepuBookDict updateOne(NepuBookDict nepuBookDict) {
        if (Validator.isEmpty(nepuBookDict.getId())){
            throw new BusinessException(ResponseCodeConst.ERROR_PARAM);
        }
        return nepuBookDictDAO.save(nepuBookDict);
    }

    @Override
    public String getServiceType() {
        return ResearchTypeConst.BOOK;
    }
}
